<list_name>=[value1,value2,value3,...,valueN]
lst=[4,3,1,6,2] print(lst) for n in lst: print(n)
[ 4, 3, 1, 6, 2 ] 4 3 1 6 2
lst=[4,3,1,6,2] s=0 for n in lst: s=s+n print("Sum is ",s)
<list_name>[index]
<list_name>[startindex:stopindex:step]
lst = [ 4, 3, 1, 6, 2, 9, 0 ] print( lst[ 2 ] ) print( lst[ 0 ] ) print( lst[ -1 ] ) print( lst[ 2 : 5 ] ) print( lst[ : 5] ) print( lst[ 2: ] ) print( lst[ : :2 ] ) print(lst[1:5:2])
1 4 0 [1,6,2] [4,3,1,6,2] [1,6,2,9,0] [4,1,2,0] [3,6]
lst = [ 4, 3, 1, 6, 2, 9, 0 ] print( lst[ -5: -2 ] ) print( lst[ -6:-2:2 ] ) print(lst[-2:-6:-2]) print( lst[ : :-1 ] )
[1,6,2] [3,6] [9,6] [0,9,2,6,1,3,4]
lst = [ 'ram','shyam','gopal','amit','raj' ] i=0 for nm in lst: print(i,"-",nm) i=i+1
0 – ram 1 – shyam 2 – gopal 3 – amit 4 – raj
enumerate(iterable, start=0)